<?php

class ContactUsForm extends BaseForm {

  const FORM_NAME = 'frm';
  const FIELD_NAME = 'name';
  const FIELD_EMAIL = 'email';
  const FIELD_PHONE = 'phone';
  const FIELD_COMMENTS = 'comments';
  const FIELD_DESCRIPTION = 'description';

  public function configure() {

    $widgets = array(
        self::FIELD_NAME => new sfWidgetFormInput(array(), array('maxlength' => 255)),
        self::FIELD_EMAIL => new sfWidgetFormInput(array(), array('maxlength' => 255)),
        self::FIELD_PHONE => new sfWidgetFormInput(array(), array('maxlength' => 30)),
        self::FIELD_COMMENTS => new sfWidgetFormTextarea(array(), array()),
        self::FIELD_DESCRIPTION => new sfWidgetFormTextarea(array(), array())
    );

    $this->setWidgets($widgets);

    $validators = array(
        self::FIELD_NAME => new sfValidatorString(array('trim' => true), array('required' => 'Please enter your Your Name')),
        self::FIELD_EMAIL => new sfValidatorEmail(array('trim' => true), array('required' => 'Please enter your Email Address', 'invalid' => 'Please enter a valid Email Address')),
        self::FIELD_COMMENTS => new sfValidatorString(array('trim' => true), array('required' => 'Please enter Comments'))
    );

    $this->setValidators($validators);
    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator'))));

    $this->widgetSchema->setNameFormat(self::FORM_NAME . '[%s]');

    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
  }

  public function postValidator($validator, $values) {
    $resp = recaptcha_check_answer(sfConfig::get('app_recaptcha_api_key_private'), $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);

    if (!$resp->is_valid) {
      throw new sfValidatorError($validator, 'Security Check failed - try again');
    }
    return $values;
  }

}

?>